package com.ruoyi.student.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.student.domain.CompetitionStudent;
import com.ruoyi.student.domain.VO.CompetitionStudentVO;
import com.ruoyi.student.mapper.CompetitionStudentMapper;
import com.ruoyi.student.service.ICompetitionStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tools.IDCardUtil;


/**
 * 报名赛事Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-15
 */
@Service
public class CompetitionStudentServiceImpl implements ICompetitionStudentService
{
    @Autowired(required = false)
    private CompetitionStudentMapper competitionStudentMapper;

    /**
     * 查询报名赛事
     * 
     * @param certno 报名赛事主键
     * @return 报名赛事
     */
    @Override
    public CompetitionStudent selectCompetitionStudentByCertno(String certno)
    {
        CompetitionStudent competitionStudent = competitionStudentMapper.selectCompetitionStudentByCertno(certno);
//        competitionStudent.setCompetitionId(competitionStudentMapper.loadCompetitionById(competitionStudent.getCompetitionId()));
        return competitionStudent;
    }

    /**
     * 查询报名赛事列表
     * 
     * @param competitionStudent 报名赛事
     * @return 报名赛事
     */
    @Override
    public List<CompetitionStudent> selectCompetitionStudentList(CompetitionStudent competitionStudent)
    {
        List<CompetitionStudent> competitionStudents = competitionStudentMapper.selectCompetitionStudentList(competitionStudent);
        for (CompetitionStudent student : competitionStudents) {
            student.setCompetitionId(competitionStudentMapper.loadCompetitionById(student.getCompetitionId()));
        }
        return competitionStudents; // 返回修改后的学生列表
    }

    /**
     * 新增报名赛事
     * 
     * @param competitionStudentvo 报名赛事
     * @return 结果
     */
    @Override
    public int insertCompetitionStudent(CompetitionStudentVO competitionStudentvo)
    {
        competitionStudentvo.setAddress(StringUtils.strip(competitionStudentvo.getRegion().toString(), "[]")
                .replaceAll(", ","") + competitionStudentvo.getAddress());
        competitionStudentvo.setCreateTime(DateUtils.getNowDate());
        String certno = competitionStudentvo.getCertno();
        competitionStudentvo.setAge(String.valueOf(IDCardUtil.getAge(certno)));
        competitionStudentvo.setSex(IDCardUtil.getSex(certno));
        System.out.println("!!!!!!!!!即将插入的值!!!!!!");
        System.out.println(competitionStudentvo);
        return competitionStudentMapper.insertCompetitionStudent(competitionStudentvo);
    }

    /**
     * 修改报名赛事
     * 
     * @param competitionStudent 报名赛事
     * @return 结果
     */
    @Override
    public int updateCompetitionStudent(CompetitionStudent competitionStudent)
    {
        competitionStudent.setUpdateTime(DateUtils.getNowDate());
        Integer review = competitionStudent.getReview();
        if(!(review == 0)){
            competitionStudent.setReviewTime(DateUtils.getNowDate());
            if(review == 1 && competitionStudentMapper.getNumbersByCerno(competitionStudent.getCertno()) == 0){
                //将审核通过的考生信息添加到exam表
                Map map = new HashMap<>();
                map.put("certno",competitionStudent.getCertno());
                map.put("name",competitionStudent.getName());
                map.put("CompetitionId",competitionStudent.getCompetitionId());
                map.put("createTime",DateUtils.getNowDate());
                competitionStudentMapper.insertCompetitionExam(map);
            }
            return competitionStudentMapper.updateCompetitionStudent(competitionStudent);
        }
        return competitionStudentMapper.updateCompetitionStudent(competitionStudent);
    }

    /**
     * 批量删除报名赛事
     * 
     * @param certnos 需要删除的报名赛事主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionStudentByCertnos(String[] certnos)
    {
        return competitionStudentMapper.deleteCompetitionStudentByCertnos(certnos);
    }

    /**
     * 删除报名赛事信息
     * 
     * @param certno 报名赛事主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionStudentByCertno(String certno)
    {
        return competitionStudentMapper.deleteCompetitionStudentByCertno(certno);
    }

    /**
     * 加载赛事列表
     * @return
     */
    @Override
    public List<Map> loadCompetition() {
        return competitionStudentMapper.loadCompetition();
    }
    @Override
    public List<Map> loadCompetitionQuestion() {
        return competitionStudentMapper.loadCompetitionQuestion();
    }


}
